package com.ymt.bpm.service.impl;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import com.ymt.bpm.dao.MDao;
import com.ymt.bpm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Service("userServiceImpl")
public class UserServiceImpl implements UserService {

	/**
	
	 * @FieldName: logger
	
	 * @Description:日志
	
	 */
	private Logger logger = Logger.getLogger(UserServiceImpl.class.getName());

	/**
	
	 * @FieldName: uDao
	
	 * @Description:人员DAO
	
	 */
	@Autowired
	private MybatisCommonDao dao;

	/**
	 * @MethodName: getUserInfo
	 * @Description: 方法说明：根据登录人的ID查询登录人信息
	 * @param loginName
	 * @return
	 * @throws Exception JSONObject
	 */
	public JSONObject getUserInfo(String loginName) throws Exception {
		logger.finer("loginName="+loginName);
		UserInfo user = dao.selectOne()
		UserInfo user = userDao.selectByLoginName(loginName);
		JSONObject rtn = (JSONObject)JSONObject.toJSON(user);
		/*//get roles
		List<RoleInfo> roles = rDao.getRoleinfoByUser(loginName);
		if (roles!=null && roles.size()>0) {
			JSONArray ra = new JSONArray();
			for (RoleInfo ri : roles) {
				JSONObject r = new JSONObject();
				r.put("roleId", ri.getRoleId());
				r.put("roleName", ri.getRoleName());
				ra.add(r);
			}
			rtn.put("roles", ra);
		}*/
		return rtn;
	}
	
	/**
	 * @MethodName: getUserInfo
	 * @Description: 方法说明：根据登录人的ID查询登录人信息
	 * @param loginName
	 * @return
	 * @throws Exception JSONObject
	 */
	@SuppressWarnings("rawtypes")
	public JSONObject getUserInfoDetail(String loginName) throws Exception {
		logger.finer("getUserInfoDetail loginName="+loginName);
		UserInfo user = userDao.selectByLoginName(loginName);
		JSONObject rtn = (JSONObject)JSONObject.toJSON(user);
		logger.finer("getUserInfoDetail rtn="+rtn);
		//get departments
		List<Map> orgs = userDao.findUserOrgInfo(loginName);
		JSONArray ra = new JSONArray();
		if (orgs!=null && orgs.size()>0) {
			for (Map org : orgs) {
				JSONObject r = new JSONObject();
				r.put("orgId", (String)org.get("org_id"));
				r.put("orgName", (String)org.get("org_name"));
				ra.add(r);
			}
		}
		rtn.put(Const.ORGS, ra);
		//get roles by userId
		List<RoleInfo> roles = rDao.getRoleinfoByUser(loginName);
		ra = new JSONArray();
		if (roles!=null && roles.size()>0) {
			for (RoleInfo ri : roles) {
				JSONObject r = new JSONObject();
				r.put("roleId", ri.getRoleId());
				r.put("roleName", ri.getRoleName());
				ra.add(r);
			}
		}
		rtn.put(Const.ROLES, ra);
		
		return rtn;
	}
	
}
